package com.ask.httpClient;



import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;


public class DeepSeekUtil {
    /**
     * V3 Url https://api.gpt.ge/v1/chat/completions
     * V3 key Bearer sk-fwQGVRA2oqYRUWhR51F88a5b43C54aCa9d2eEd3b229c8cE9
     * V3 json
     */
    private static final String BASE_URL = "https://api.gpt.ge/v1/chat/completions";
    private static final String API_KEY = "Bearer sk-fwQGVRA2oqYRUWhR51F88a5b43C54aCa9d2eEd3b229c8cE9";


    /**
     * 发送消息 至 V3 api
     * @throws IOException
     */
    public static void sendMessage() throws IOException {
        // 构建请求体body
        JSONObject message = new JSONObject();
        message.put("role", "user");
        message.put("content", "你好");

        JSONArray messages = new JSONArray();
        messages.put(message);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model", "gpt-3.5-turbo");
        jsonObject.put("messages", messages);
        jsonObject.put("max_tokens", 1688);
        jsonObject.put("temperature", 0.5);
        jsonObject.put("stream", false);


        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpPost post = new HttpPost(BASE_URL);
        post.setHeader("Content-Type", "application/json");
        post.setHeader("Authorization", API_KEY);

        // 设置请求体
        StringEntity entity = new StringEntity(jsonObject.toString(), "UTF-8");
        post.setEntity(entity);

        // 执行请求
        CloseableHttpResponse response = httpClient.execute(post);
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.printf("状态响应码为："  + statusCode);

        if (statusCode != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + statusCode);
        }

        // 获取解析并响应
        HttpEntity entity1 = response.getEntity();
        String body = EntityUtils.toString(entity1);
        System.out.printf("响应数据为：" + body);
        response.close();
        httpClient.close();
    }

    public static void main(String[] args) throws IOException {
        sendMessage();
    }

}
